package jdbc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.UUID;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import jdbc.entity.ShopEntity;
import jdbc.service.ShopService;

public class App {
	private static ApplicationContext ac = new ClassPathXmlApplicationContext("jdbc/applicationContext.xml");
	ShopService shopService = ac.getBean("shopService", ShopService.class);
	
	@Test
	public void addShop() {
//		String goodsCode = UUID.randomUUID().toString();
//		Goods goods = new Goods("goodsName", goodsCode, 100);
//		userService.addGoods(goods);
//
//		// 查询库中的数据
//		Goods returnGoods = userService.getGoods(goodsCode);
//		assertEquals(goods.getGoodsName(), returnGoods.getGoodsName());
//		assertEquals(goods.getGoodsCode(), returnGoods.getGoodsCode());
//		assertEquals(goods.getPrice(), returnGoods.getPrice());
		String sName=UUID.randomUUID().toString();
		ShopEntity shop=new ShopEntity(sName,"CODE001","11","张三");
		shopService.addShop(shop);
		ShopEntity returnShopEntity=shopService.getShop(sName);
		assertEquals(shop.getsName(),returnShopEntity.getsName());
		assertEquals(shop.getsNumber(),returnShopEntity.getsNumber());
		assertEquals(shop.getsPrice(),returnShopEntity.getsPrice());
	}
	public ShopEntity addAndGetShopEntity() {
//		String goodsCode = UUID.randomUUID().toString();
//		Goods goods = new Goods(UUID.randomUUID().toString(), goodsCode, 100);
//		userService.addGoods(goods);
		String sName=UUID.randomUUID().toString();
		ShopEntity shopEntity=new ShopEntity();
		shopService.addShop(shopEntity);
		return shopEntity;
		
	}
	@Test
	public void deleteShop() {
		ShopEntity initShop=addAndGetShopEntity();
		assertEquals(1, shopService.deleteShop(initShop.getsName()));
		// 检查是否已经删除
		assertNull(shopService.getShop(initShop.getsName()));
	}
	@Test
	public void updateShop() {
		ShopEntity shop = new ShopEntity();
		shop.setsName("sName");
		shop.setsNumber("sNumber");
		shop.setsPrice("sPrice");
		assertEquals(1, shopService.updateShop(shop));
	}
	@Test
	public void listShop() {
		ShopEntity shop = new ShopEntity();
		shop.setsName("sName");
		shop.setsNumber("sNumber");
		shop.setsPrice("sPrice");
		shop.setDeleteLogo("deleteLogo");
		shop.setCreateName("createName");
		shop.setUpdateName("updateName");
		List<ShopEntity> shopList = shopService.listShop();
		shopList.forEach(System.out::println);
		assertTrue(shopService.listShop().size() > 0);
	}
	@Test
	public void count() {
		assertTrue(shopService.count()>0);
	}
	@Test 
	public void getShop() {
		ShopEntity initShop = addAndGetShopEntity();
		assertNotNull(shopService.getShop(initShop.getsName()));
	}
}
